package com.example.demo.service.Impl;

import com.example.demo.entity.Image;
import com.example.demo.mapper.SelectImage;
import com.example.demo.service.ReptileSelect;
import com.example.demo.utils.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ReptileSelectImpl implements ReptileSelect {
    @Override
    public void Reptile(String https) throws Exception{
        HttpUtils httpUtils =new HttpUtils();

//        String url="https://www.hexuexiao.cn/meinv/qingchunmeinv/list-1.html";
        String url=https;
        String html =httpUtils.doGetHtml(url);
        parse(html);
    }

//    数据处理
    public void parse(String html) throws Exception{

        SelectImage image=new SelectImage();
        Image item=new Image();
        Document doc= Jsoup.parse(html);
        Elements DivImages=doc.getElementsByClass("waterfall_1box");

        for(Element divimage:DivImages) {

            String Imagehttp = divimage.select("img").attr("src");
            String title = divimage.select("img").attr("title");

//            String basehttp = divimage.select("dd a").attr("href");


            if (Imagehttp == "" || title == "" ) {
                continue;
            } else {

//                原图
                String FormerImaehttp=setFormerImagehttp(Imagehttp);
                System.out.println(Imagehttp + "主题：" + title + "原图   " +FormerImaehttp);
                item.setImagehttps(Imagehttp);
                item.setTitle(title);
                item.setType(1);
                item.setFormerImagehttp(FormerImaehttp);
                image.addImage(item);
            }

        }
    }

//    public String baseImageparse(String html){
//        Document doc= Jsoup.parse(html);
//        Element DivImage=doc.getElementsByClass("img-responsive center-block").first();
//
//
//        String baseImage=DivImage.attr("src");
//
//
//        return baseImage;
//    }

//    获得原图
    public String setFormerImagehttp(String Imagehttp){
        String FormerImaehttp=Imagehttp.substring(0,Imagehttp.length()-8);
        return FormerImaehttp;
    }

}
